<?php
/*  goat-cms : tumbleblog content management system
    Copyright (C) 2008 Thomas Purnell tom@thomaspurnell.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

function gc_displayEntry($entry){
	switch ($entry['TYPE']){
		case "Audio":
			echo gc_display_audio($entry);
			break;
		case "Image":
			echo gc_display_image($entry);
			break;
		case "Quote":
			echo gc_display_quote($entry);
			break;
		case "Link":
			echo gc_display_link($entry);
			break;
		case "Video":
			echo gc_display_video($entry);
			break;
		case "Manual":
			echo gc_display_manual($entry);
			break;
	}
}

function gc_source($entry){
	if (strlen($entry['SOURCEURL']) > 3){
		if (strlen($entry['SOURCETEXT']) > 0){
			return "<a class=\"gc_source_link\" href=\"".$entry['SOURCEURL']."\">".$entry['SOURCETEXT']."</a>";
		} else {
			return "<a class=\"gc_source_link\" href=\"".$entry['SOURCEURL']."\">".$entry['SOURCEURL']."</a>";
		}
	} else {
		if (strlen($entry['SOURCETEXT']) > 0){
			return "<cite class=\"gc_source_linkless\">".$entry['SOURCETEXT']."</cite>";
		} else {
			return "No source provided";
		}
	}
}

function gc_display_audio($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_audio\">";
	$result .= "<div class=\"gc_title gc_title_audio\"><h2>". $entry['SUBTIME'] . " : " . $entry['TITLE'] ."</h2></div>";
	$result .= "<div class=\"gc_content gc_content_audio\">[AUDIO EMBED]".$entry['CONTENTA']."</div>";
	$result .= "<div class=\"gc_source gc_source_audio\">" . gc_source($entry) . "</div>";	
	$result .= "<div class=\"gc_caption gc_caption_audio\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

function gc_display_image($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_image\">";
	$result .= "<div class=\"gc_title gc_title_image\"><h2>". $entry['SUBTIME'] . " : " . $entry['TITLE'] ."</h2></div>";
	$result .= "<div class=\"gc_content gc_content_image\"><img src=\"".$entry['CONTENTA']."\" alt=\"".$entry['TITLE']."\" /></div>";
	$result .= "<div class=\"gc_source gc_source_image\">".gc_source($entry)."</div>";	
	$result .= "<div class=\"gc_caption gc_caption_image\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

function gc_display_quote($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_quote\">";
	$result .= "<div class=\"gc_title gc_title_quote\"><h2>". $entry['SUBTIME'] . " : " . substr( $entry['CONTENTA'], 0, 12 ) ."...</h2></div>";
	$result .= "<div class=\"gc_content gc_content_quote\"><blockquote><p>".$entry['CONTENTA']."</p></blockquote></div>";
	$result .= "<div class=\"gc_source gc_source_quote\">".gc_source($entry)."</div>";	
	$result .= "<div class=\"gc_caption gc_caption_quote\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

function gc_display_link($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_link\">";
	$result .= "<div class=\"gc_title gc_title_link\"><h2>". $entry['SUBTIME'] . " : " . $entry['TITLE'] ."</h2></div>";
	$result .= "<div class=\"gc_content gc_content_link\"><a target=\"_blank\" href=\"".$entry['CONTENTA']."\">".$entry['CONTENTA']."</div>";
	$result .= "<div class=\"gc_source gc_source_link\">".gc_source($entry)."</div>";	
	$result .= "<div class=\"gc_caption gc_caption_link\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

function gc_display_video($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_video\">";
	$result .= "<div class=\"gc_title gc_title_video\"><h2>". $entry['SUBTIME'] . " : " . $entry['TITLE'] ."</h2></div>";
	$result .= "<div class=\"gc_content gc_content_video\">".gc_youtube_embed( gc_extract_youtubeID( $entry['CONTENTA'] ) )."</div>";
	$result .= "<div class=\"gc_source gc_source_video\">".gc_source($entry)."</div>";	
	$result .= "<div class=\"gc_caption gc_caption_video\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

function gc_display_manual($entry){
	$result = "<div id=\"". $entry['ID'] . "\" class=\"gc_entry gc_entry_manual\">";
	$result .= "<div class=\"gc_title gc_title_manual\"><h2>". $entry['SUBTIME'] . " : " . $entry['TITLE'] ."</h2></div>";
	$result .= "<div class=\"gc_content gc_content_manual\">".$entry['CONTENTA']."</div>";
	$result .= "<div class=\"gc_source gc_source_manual\">".gc_source($entry)."</div>";	
	$result .= "<div class=\"gc_caption gc_caption_manual\">".$entry['CAPTION']."</div>";
	$result .= "</div>";
	return $result;
}

//Outputs youtube suggested object/embed for youtube video player with the provided youtube video ID ($ytID)
function gc_youtube_embed($ytID){
	return '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/'.$ytID.'&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$ytID.'&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>';
}

//Extracts the value of 'v' from a youtube URL, which should give us the video ID
function gc_extract_youtubeID( $ytURL ){
	$ytArgs;
	$urlComponents = parse_url($ytURL);
	parse_str( $urlComponents['query'], $ytArgs );
	return $ytArgs['v'];
}

?>
